<template>
	<div>
		<!--高级查询-->
		<el-form :inline="true" :model="query" class="demo-form-inline">
			<el-form-item label="名字">
				<el-input v-model="query.keyword" placeholder="根据名字关键字搜索"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="search">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="edit(null)">新增</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="batchDelete">删除</el-button>
			</el-form-item>
		</el-form>
		<el-table
				:data="pageInfo.rows"
				border
				stripe
				style="width: 100%; text-align: center"
				@selection-change="handleSelectionChange">
			<el-table-column
					type="selection"
					width="%5">
			</el-table-column>
			<el-table-column type="index" width="%5" sortable>
			</el-table-column>
			<!--<el-table-column
					prop="id"
					width="10"
					sortable>
			</el-table-column>
-->
			<el-table-column
					prop="name"
					label="姓名"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="intro"
					label="简介"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="parent.name"
					label="上级部门"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="manager.username"
					label="部门经理"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="create_time"
					label="创建时间"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="update_time"
					label="更新时间"
					width="%10"
					sortable>
			</el-table-column>
			<el-table-column
					prop="state"
					label="状态"
					width="%10"
					sortable>
				<template slot-scope="scope">
					<span v-if="scope.row.state==1" style="color: green">正常</span>
					<span v-else style="color: red">禁用</span>
				</template>
			</el-table-column>

			<el-table-column
					label="操作"
					sortable>
				<template slot-scope="scope">
					<el-button @click="deleteById(scope.row)" type="text" style="color: red" size="small">删除</el-button>
					<el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="block">
			<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page.sync="query.currentPage"
					:page-sizes="[5, 8, 12, 15]"
					:page-size="query.pageSize"
					layout="sizes, prev, pager, next"
					:total="pageInfo.total">
			</el-pagination>
		</div>
		<el-dialog title="新增/修改" v-loading="loading" :visible.sync="dialogFormVisible">

			<el-form :model="department">
				<el-form-item label="部名" label-width="120px">
					<el-input v-model="department.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门经理" label-width="120px">
						<!--下拉选项
							 :key=""  //唯一标识
							 :label 选择之后展示到选择框中的值
							 :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
								必须要写  value-key="id"
						   -->
					<el-select v-model="department.manager" clearable value-key="id" placeholder="请选择">
						<el-option
								v-for="employee in employees"
								:key="employee.id"
								:label="employee.username"
								:value="employee">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="上级部门" label-width="120px">
						<el-cascader
								v-model="department.parent"
								:options="departments"
								:props="{
								   checkStrictly: true,
								   label:'name',
								   value:'id'
								}"></el-cascader>
				</el-form-item>
				<el-form-item label="状态" label-width="120px">
					<el-radio-group v-model="department.state">
						<el-radio  :label="1">正常</el-radio>
						<el-radio :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="简介" label-width="120px">
					<el-input type="textarea" v-model="department.intro" autocomplete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="cancle">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</div>
		</el-dialog>
	</div>

</template>

<script>
	export default {
		name: "DepartmentPage",
		data() {
			return {
				query: {
					currentPage: 1,
					pageSize: 5,
					keyword: ""
				},
				pageInfo: {
					rows: null,
					total: null
				},
				dialogFormVisible: false,
				department: {
					id: null,
					name: "",
					intro: "",
					parent: {
						id: null,
						name: ""
					},
					manager: {
						id: null,
						username: ""
					},
					create_time: null,
					update_time: null,
					path: null,
					state: null
				},
				loading: false,
				selections: [],
				departments:[],
				employees:[],
				parent:{
					id:null,
					name:""
				}
			}
		},
		methods: {
			search() {
				this.query.currentPage=1
				this.getDepartments();
			},
			batchDelete(){
				if(this.selections.length<=0){
					this.getMessage(1,"请先选择要删除的数据！！！")
				}else {
					this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						let ids = this.selections;
						this.$http.patch("/department", ids).then(result => {
							if (result.data.success) {
								this.getMessage(0, result.data.message);
								this.query.currentPage=1
								this.getDepartments();
							} else {
								this.getMessage(2, result.data.message);
							}
						}).catch(error => {
							this.getMessage(1, error.message)
						})
					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					})
				}
			},
			cancle(){
				this.department.parent = this.parent;
				this.dialogFormVisible = false;
			},
			edit(val) {
				if (!val) {
					this.department = {
						id: null,
						name: "",
						intro: "",
						parent: {
							id: null,
							name: ""
						},
						manager: {
							id: null
						},
						create_time: null,
						update_time: null,
						create_user: {
							id: null
						},
						update_user: {
							id: null
						}
					}
				} else{
					this.department=val
					let ids = this.department.path.split("/");
					this.parent = this.department.parent;
					this.department.parent=[];
					for(let i = 1;i<ids.length-1;i++){
						this.department.parent.push(parseInt(ids[i]))
					}
				}

				this.$http.get("/employee").then(result=>{
					result = result.data;
					if(result.success){
						this.employees = result.result;
					}else{
						this.getMessage(0,result.message)
					}
				}).catch(error=>{
					this.getMessage(1,error.message)
				})
				this.$http.post("/department/tree",this.department).then(result=>{
					result = result.data;
					if(result.success){
						this.departments = result.result;
						console.log(this.departments)
					}else{
						this.getMessage(0,result.message)
					}
				}).catch(error=>{
					this.getMessage(1,error.message)
				})
				this.dialogFormVisible = true;

			},
			save() {
				if(this.department.parent.length>0){
					let ids = this.department.parent;
					this.department.parent={
						id: ids[ids.length-1]
					}
				}
				let dep = this.department;
				debugger
				this.$http.put("/department", this.department)
						.then(result => {
							this.loading = true;
							if (result.data.success) {
								this.getMessage(0, result.data.message);
								this.dialogFormVisible = false;
								this.query.currentPage=1;
								this.getDepartments();
								this.loading = false
							} else {
								this.getMessage(2, result.data.message);
								this.loading = false
							}
						})
						.catch(error => {
							this.getMessage(1, error.message);
							this.loading = false
						})
			},
			deleteById(val) {
				this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http.delete("/department/"+val.id).then(result => {
						if (result.data.success) {
							this.getMessage(0, result.data.message);
							this.query.currentPage=1
							this.getDepartments();
						} else {
							this.getMessage(2, result.data.message);
						}
					}).catch(error => {
						this.getMessage(1, error.message);
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				})
			},
			getMessage(type, message) {
				if (type == 0) {
					this.$message({
						message: message,
						type: 'success'
					});
				} else if (type == 1) {
					this.$message.error(message);
				} else if (type == 2) {
					this.$message(message)
				}
			},
			handleSizeChange(val) {
				this.query.pageSize = val;
				this.query.currentPage = 1;
				this.getDepartments();
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getDepartments();
			},
			handleSelectionChange(val) {

				console.log(val)
				let ids = val.map(map => map.id)
				this.selections = ids;
			},
			handleClick(val) {
				console.log(val)
			},
			getDepartments() {
				this.$http.post('/department', this.query).then(result => {
					result = result.data
					this.pageInfo= result.result;
					console.log(result)
					console.log(this.pageInfo)
				})
			}
		},
		mounted() {
			this.getDepartments();
		}
	}

</script>

<style scoped>

</style>